<template>
  <div>
    <BasicModal
      width="80%"
      v-bind="$attrs"
      @register="registerModal"
      :title="getTitle"
      @ok="handleSubmit"
    >
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { AddApi, EditApi, ListApi } from '/@/api/user/salesUser';
  import { FormSchema } from '/@/components/Table';
  import { genderTypes } from '/@/assets/dic';
  import { useMessage } from '/@/hooks/web/useMessage';

  export const formSchema: FormSchema[] = [
    {
      label: 'id',
      field: 'id',
      component: 'Input',
      show: false,
    },
    {
      field: 'name',
      label: '姓名:',
      component: 'Input',
      required: true,
      colProps: { lg: 24, md: 24 },
    },
    {
      field: 'gender',
      label: '性别:',
      component: 'Select',
      componentProps: {
        options: genderTypes,
      },
      // required: true,
      defaultValue: genderTypes[0].value,
    },
    {
      field: 'phone',
      label: '手机号:',
      component: 'Input',
      required: true,
      colProps: { lg: 24, md: 24 },
    },
    {
      field: 'account',
      label: '微信号:',
      component: 'Input',
      required: true,
      colProps: { lg: 24, md: 24 },
    },
    // {
    //   field: 'isUse',
    //   label: '状态:',
    //   component: 'RadioButtonGroup',
    //   defaultValue: '0',
    //   componentProps: {
    //     options: [
    //       { label: '启用', value: '0' },
    //       { label: '禁用', value: '1' },
    //     ],
    //   },
    // },
  ];

  export default defineComponent({
    name: 'SalesUserModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const { createMessage } = useMessage();
      const { error } = createMessage;
      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 160,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: { lg: 12, md: 24 },
      });

      const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data) => {
        resetFields();
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          setFieldsValue({
            ...data.record,
          });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增销售人员' : '编辑销售人员'));

      function fn() {
        createMessage.error('用户已存在');
      }

      async function handleSubmit() {
        try {
          setModalProps({ confirmLoading: true });
          let Names = <any>[];
          await ListApi({ pageNum: 1, pageSize: 10 }).then((res) => {
            res.rows.map((item) => {
              Names.push(item.name);
            });
          });
          const values = await validate();
          // values.parentId = 0 | values.parentId;
          if (values.id > 0) {
            await EditApi(values);
          } else {
            if (Names.indexOf(values.name) === -1) {
              await AddApi(values);
            } else {
              fn();
              Names = [];
            }
          }
          console.log(values);
          closeModal();
          emit('success');
        } catch (err) {
          console.log(err);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { registerModal, registerForm, getTitle, handleSubmit, errorMsg: error };
    },
  });
</script>
